//
//  MPVisitorView.swift
//  MPSinaWeibo
//
//  Created by DaVinci on 16/4/7.
//  Copyright © 2016年 DaVinci. All rights reserved.
//

import UIKit
import SnapKit

@objc protocol MPVisitorViewDelegate:NSObjectProtocol {

   optional func rigisterBtnWillClick()

   optional func loginBtnWillClick()
}

class MPVisitorView: UIView {
   
    
    weak var delegate: MPVisitorViewDelegate?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
   }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
    
    //MARK:设置游客登录信息
    
    func setVisitorInfo(tipText: String,imageName: String,isHome: Bool = false){
    
        if isHome{
        
           startAnimation()
            
        }else{
        
          house.hidden = true
          bringSubviewToFront(circleIcon)
        }
       
        
        tipLabel.text = tipText
        circleIcon.image = UIImage(named: imageName)
    }
    
    //MARK:设置动画
    private func startAnimation(){
    
       let animation = CABasicAnimation()
        animation.keyPath = "transform.rotation"
        
        animation.repeatCount = MAXFLOAT
        
        animation.toValue = M_PI * 2
        
        animation.duration = 13
        
        animation.removedOnCompletion = false
        
        circleIcon.layer.addAnimation(animation, forKey: nil)
    
    }
    
    //MARK:设置控件
    private func setupUI(){
    
        addSubview(circleIcon)
        addSubview(mask)

        addSubview(house)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        circleIcon.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        
        house.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(circleIcon.snp_center)
        }
        
        tipLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.centerX.equalTo(circleIcon.snp_centerX)
            
            make.height.equalTo(40)
            make.width.equalTo(230)
        }
        
        loginBtn.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(17)
            
            make.width.equalTo(100)
        }
        
        registerBtn.snp_makeConstraints { (make) -> Void in
            
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(17)
            make.width.equalTo(100)
        }
        
        mask.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(circleIcon.snp_center)
        }
        
        let value: CGFloat = 237.0 / 255.0
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1.0)
        
        registerBtn.addTarget(self, action: "rigisterBtnClick", forControlEvents: .TouchUpInside)
        loginBtn.addTarget(self, action: "loginBtnClick", forControlEvents: .TouchUpInside)
    }
    
    @objc private func rigisterBtnClick(){
    
        delegate?.rigisterBtnWillClick?()
       
        
    }
    
    @objc private func loginBtnClick(){
    
        

        delegate?.loginBtnWillClick?()
    }
    

    
    //MARK:蒙版懒加载
    private lazy var mask: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
   private lazy var circleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
   private lazy var house: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //MARK:提示栏懒加载
    private lazy var tipLabel: UILabel = {
    
        let l = UILabel()
        l.textColor = UIColor.darkGrayColor()
        l.text = "关注一些人,回到这里看看有什么惊喜,回到这里看看有什么惊喜"
        l.numberOfLines = 0;
        l.font = UIFont.systemFontOfSize(14)
        l.textAlignment = .Center
        
        l.sizeToFit()
        
        return l
    }()
    
    //MARK:登录按钮懒加载
    private lazy var loginBtn: UIButton = {
    
        let btn = UIButton()

        btn.setTitle("登录", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        
        var image = UIImage(named: "common_button_white_disable")
        let cW: Int = Int((image?.size.width)! * 0.5)
        let cH: Int = Int((image?.size.height)! * 0.5)
    
        image = image?.stretchableImageWithLeftCapWidth(cW, topCapHeight: cH)
        
        btn.setBackgroundImage(image, forState: .Normal)
        
        return btn
    }()
    
    //MARK:注册按钮懒加载
    private lazy var registerBtn: UIButton = {
     
        let btn = UIButton()
        
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        var image = UIImage(named: "common_button_white_disable")!
        let capW = Int(image.size.width * 0.5)
        let capH = Int(image.size.height * 0.5)
        image = image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH)
        
        btn.setBackgroundImage(image, forState: .Normal)
        
        return btn
    
    }()
}
